package com.rui.dao.impl;

import com.rui.dao.ManagerDao;
import com.rui.entity.Manager;
import org.apache.commons.lang.StringUtils;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import java.util.List;

/**
 * Created by Administrator on 2017/4/6.
 */
@Repository
public class ManagerDAOImpl implements ManagerDao {

    @Autowired
    private SessionFactory sessionFactory;

    private Session getSession(){
        return sessionFactory.getCurrentSession();
    }

    public void add(Manager manager) {
        getSession().beginTransaction();
        getSession().save(manager);
        getSession().getTransaction().commit();
    }

    public void delete(Integer id) {
        getSession().beginTransaction();
        Manager manager = (Manager) getSession().get(Manager.class, Integer.valueOf(id));
        if (manager != null){
            getSession().delete(manager);
        }
        getSession().getTransaction().commit();
    }

    public Manager findOne(Integer id) {
        getSession().beginTransaction();
        Manager manager = (Manager) getSession().get(Manager.class, Integer.valueOf(id));
        getSession().getTransaction().commit();
        return manager;
    }

    public void update(Manager manager) {
        getSession().beginTransaction();
        getSession().update(manager);
        getSession().getTransaction().commit();
    }

    public List<Manager> findManagers(String userName, Integer pageSize, Integer pageIndex) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("from Manager where 1 = 1");
        if (StringUtils.isNotBlank(userName)){
            sb.append(" and userName like '%" + userName + "%'");
        }
        Query query = getSession().createQuery(sb.toString());
        query.setFirstResult((pageIndex-1)*pageSize);
        query.setMaxResults(pageSize);
        List<Manager> managers = query.list();
        getSession().getTransaction().commit();
        return managers;
    }

    public Integer findTotalPage(String userName, Integer pageSize) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("select count(id) from Manager where 1 = 1");
        if (StringUtils.isNotBlank(userName)){
            sb.append(" and userName like '%" + userName + "%'");
        }
        Query query = getSession().createQuery(sb.toString());
        Long count = (Long) query.uniqueResult();
        getSession().getTransaction().commit();
        if (count%pageSize==0){
            return count.intValue() / pageSize;
        }else{
            return count.intValue() / pageSize + 1;
        }
    }

    public Manager findByUserName(String userName) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("from Manager where userName = '" + userName + "'");
        Query query = getSession().createQuery(sb.toString());
        List<Manager> managers = query.list();
        getSession().getTransaction().commit();
        if (managers.size()>0) {
            return managers.get(0);
        }else {
            return null;
        }
    }

    public List<Manager> findManagers(String userName, Integer id) {
        getSession().beginTransaction();
        StringBuilder sb = new StringBuilder("from Manager where 1 = 1");
        if (StringUtils.isNotBlank(userName)){
            sb.append(" and userName = '" + userName + "'");
        }
        if (id != null){
            sb.append(" and id != " + id);
        }
        Query query = getSession().createQuery(sb.toString());
        List<Manager> managers = query.list();
        getSession().getTransaction().commit();
        return managers;
    }
}
